<?php
header('Content-Type: application/json; charset=utf-8');

// 获取用户输入的文件格式
$file_format = $_POST['file_format'] ?? '';

// 如果没有输入文件格式，返回错误提示
if (empty($file_format)) {
    echo json_encode(['status' => 'error', 'message' => '请输入资源格式错误'], JSON_UNESCAPED_UNICODE);
    exit();
}

// 连接数据库
try {
    $dbConfig = include('db_config.php');
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 查询语句：通过文件格式筛选记录
    $querySQL = "
        SELECT * FROM mi_file WHERE 文件格式 = :file_format;
    ";

    // 使用预处理语句查询
    $stmt = $pdo->prepare($querySQL);
    $stmt->bindParam(':file_format', $file_format);
    $stmt->execute();

    // 获取所有查询结果
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // 返回结果
    if (empty($result)) {
        echo json_encode(['status' => 'success', 'message' => '空'], JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['status' => 'success', 'message' => '成功', 'data' => $result], JSON_UNESCAPED_UNICODE);
    }

} catch (PDOException $e) {
    // 捕获异常并返回错误信息
    $errorResult = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($errorResult, JSON_UNESCAPED_UNICODE);
}
